package com.cskaoyan.service.wx.zjh;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cskaoyan.config.zjhShiro.MallToken;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bean.vo.VxLoginVO;
import com.cskaoyan.module.pojo.MarketUser;
import com.cskaoyan.module.pojo.MarketUserExample;
import com.cskaoyan.module.vo.wx.zjh.UserInfoEntity;
import com.sun.xml.bind.v2.model.core.ID;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:
 * @Author: 周佳豪
 * @Date: 2022/04/12/9:22
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    MarketUserMapper userMapper;

    BaseRespVo baseRespVo = new BaseRespVo();

    String phone = null;
    String phoneCode = null;

    @Override
    public BaseRespVo authRegCaptcha(String mobile) {
        phone = mobile;
        String accessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";
        String accessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";
        String signName = "stone4j";
        String templateCode = "SMS_173765187";
        String phoneNumber = mobile;

        Random random = new Random();
        Integer num = random.nextInt(1000000) - 100000;
        phoneCode = String.valueOf(num);

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + phoneCode + "\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        baseRespVo.setData(null);
        return baseRespVo;
    }

    @Override
    public BaseRespVo authRegister(String code, String mobile, String password, String username, String wxCode) {
        MarketUser marketUser = new MarketUser();
        BaseRespVo<Object> b = new BaseRespVo<>();
        //判断验证码是否正确
        if (!code.equals(phoneCode)) {
            b.setErrno(703);
            b.setErrmsg("验证码错误");
            return b;
        }
        //先判断数据库是否已存在该手机号
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        criteria.andMobileEqualTo(mobile);
        List<MarketUser> marketUsers = userMapper.selectByExample(example);
        if (marketUsers.size() != 0) {
            b.setErrno(703);
            b.setErrmsg("手机号已被注册");
            return b;
        }
        //判断用户名是否已被注册
        MarketUserExample example2 = new MarketUserExample();
        MarketUserExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andUsernameEqualTo(username);
        List<MarketUser> marketUsers2 = userMapper.selectByExample(example2);
        if (marketUsers2.size() != 0) {
            b.setErrno(703);
            b.setErrmsg("用户名已被注册");
            return b;
        }
        //最后注册用户，成功
        marketUser.setUsername(username);
        marketUser.setPassword(password);
        marketUser.setNickname(username);
        marketUser.setGender(Byte.valueOf("1"));
        marketUser.setLastLoginIp("127.0.0.1");
        marketUser.setMobile(mobile);
        marketUser.setAvatar("https://project2file.oss-cn-beijing.aliyuncs.com/072c3064-14e5-4d7f-adda-150d767d3957OIP-C.png");
        marketUser.setWeixinOpenid("0.0.0.1");
        marketUser.setSessionKey(wxCode);
        marketUser.setStatus(Byte.valueOf("0"));

//        用完验证码之后让验证码为空
        phoneCode = null;

        //注册的代码，后面就可以查到了
        userMapper.insert(marketUser);

        Subject subject = SecurityUtils.getSubject();
        subject.login(new MallToken(username, password, "wechat"));

        baseRespVo.setData(marketUser);
        baseRespVo.setErrno(0);
        baseRespVo.setErrmsg("成功");
        return baseRespVo;
    }

    @Override
    public BaseRespVo authReset(String code, String mobile, String password) {
        BaseRespVo<Object> b = new BaseRespVo<>();
        if (!code.equals(phoneCode)) {
            b.setErrno(703);
            b.setErrmsg("验证码错误");
            return b;
        }
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andMobileEqualTo(mobile);

        List<MarketUser> marketUsers = userMapper.selectByExample(marketUserExample);

        MarketUser marketUser = marketUsers.get(0);
        marketUser.setPassword(password);

        userMapper.updateByPrimaryKeySelective(marketUser);
//        用完验证码之后让验证码为空
        phoneCode = null;

        b.setErrno(0);
        b.setErrmsg("成功");
        return b;
    }
}
